/**
 * mqtt@4.3.7
 */
const mqtt = require('mqtt')
M=require("ming_node")


// 连接选项
const options = {
    connectTimeout: 4000, // 超时时间
    // 认证信息
     clientId: 'emqx-connect-via-websocket1',
     username: 'DINGYUNBO ',
     password: 'DINGYUNBO123',
}

var client;

client = mqtt.connect('tcp://47.94.53.79:1883', options);

client.on('reconnect', (error) => {
    console.log('正在重连:', error)
})

client.on('error', (error) => {
    console.log('连接失败:', error)
})

client.on('connect', (e) => {
    console.log('成功连接服务器')
    // 订阅主题 下面的#是订阅XXXXX下面的所有主题
    client.subscribe('/ZCZG/PEGC/ABCD/5200/SCENARIO', {
        qos: 1
    }, (error) => {
        if (!error) {
            console.log('订阅成功')
        }
    })
})

client.on('message', (topic, message) => {
    console.log('收到来自', topic, '的消息', message.toString())

})


var send = function(topic, jsonData) {
    client.publish(topic, JSON.stringify(jsonData), {
        qos: 1,
        rein: false
    }, (error) => {
        if(!error){

        }
        console.log(error || '发布成功')
    })
}
setInterval(()=>{
    send('/langjieMaxTestTopic',{
        "a我挖的伟大a":88
    })

},500)


const app= M.server();

app.listen(8888)